<?php
namespace app\app\util;
use think\Request;
use think\cache\driver\Redis;
//排行榜
define('CONTRIBUTE','ft_user_cont');

class ShareUtils{

    //创建投票数
    public static function foundUserCont($user_id,$number=0)
    {
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        //创建用户
        return $redis->zadd(CONTRIBUTE,$number,$user_id);
    }

    //添加投票数/获取数目
    public static function getUserCont($user_id,$number=0)
    {
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($number == 0){
            return $redis->zscore(CONTRIBUTE,$user_id);
        }else{
            $result = $redis->zincrby(CONTRIBUTE,$number,$user_id);
            return $result;
        }
    }

    //获取指定范围排名
    public static function getContRank($start,$end,$type='')
    {
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($type == ''){
            return $redis->zrevrange(CONTRIBUTE,$start,$end);
        }else{
            $type = 'withscores';
            return $redis->zrevrange(CONTRIBUTE,$start,$end,$type);
        }
    }

}